﻿using System;
using System.Web.UI;
using Emz.Core.Models;
using Legazpi.Payroll.Services.Interfaces;
using Ninject;
using Legazpi.Payroll.Web.Helpers;

namespace Legazpi.Payroll.Web.Usercontrols
{
    public partial class EmployeeAddControl : System.Web.UI.UserControl
    {

        [Inject]
        public IEmployeeService _employeeService { get; set; }

        protected Employee Model { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int id;
                int.TryParse(Request.QueryString["id"], out id);
                if (id > 0)
                    //Update Data
                    Model = _employeeService.GetById(id);
            }
            //New Data
            Model = Model ?? new Employee { Id = 0 };

            if (!Page.IsPostBack)
                BindDropdowns();
        }

        #region Implementations
        void BindDropdowns()
        {
            //Positions                                              
            PositionDropdown.ToPositionSelectList(Model.PositionId);

            //Clients/Deployed At            
            DeployedDropdown.ToSectorSelectList(Model.DetachmentId);

            //Supervisors
            SupervisorDropdown.ToSuperVisorSelectList(Model.SupervisorId.GetValueOrDefault(0));

            //Employee Status
            EmployedStatusDropDown.ToEmployeeStatusSelectList(Model.EmployedStatus);
        }

        void NotifySuccessSave()
        {
            
            ClientScriptManager cs = Page.ClientScript;
            var scriptName = "notification";
            Type cstype = this.GetType();
            if (!cs.IsStartupScriptRegistered(scriptName))
            {
                string script = @"    
                    $(document).ready(function () {
                        PopUpDialog.MessageDialog(""Success!"", 350, 100, true, ""<center>Saved!</center>"", ""Saved"");
                    });";

                cs.RegisterStartupScript(cstype, scriptName, script, true);
            }

        }
        #endregion

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(ResolveUrl("~/Admin/Employees.aspx"));            
        }

    }
}
